package itsm.isperp.report.resolver;

import itsm.isperp.module.entity.kpi.KpiSetting;
import itsm.isperp.report.Counter;
import itsm.isperp.report.analyzer.SortAnalyzer;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.data.SeriesData;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Gauge;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Pie;
import com.github.abel533.echarts.series.Series;
import com.google.common.collect.HashBasedTable;

/**
 * 
 * 比较解析器
 * 
 * @author lizx
 * @date 2015-9-6
 * @since 1.0
 * 
 */
public class DataResolver extends BaseResolver {

	public DataResolver(KpiSetting setting, String dtype, String group,
			String stype, boolean queryLine, HttpServletRequest req)
			throws ParseException {
		super(setting, dtype, group, stype, queryLine, req);

	}

	protected GsonOption resolveValue() {

		queryRequest.setWhere(queryRequest.getWhere() + sb.toString());

		queryRequest.setOrderBy("value DESC");

		List<Map<String, Object>> list = queryRequest.query();

		GsonOption option = new GsonOption();

		Series<?> p = null;
		if (this.group == null) {

			p = new Pie();
			p.data().addAll(list);

			for (Map<String, Object> m : list) {
				option.legend()
						.data()
						.add(m.get("name") == null ? Counter.LABEL_NULL : m
								.get("name"));
				option.legend().orient(Orient.vertical);
				option.legend().setX(0);
				option.legend().setY(0);
			}

		} else {
			p = new Bar();
			CategoryAxis x = new CategoryAxis();
			for (Map<String, Object> m : list) {
				x.data().add(
						m.get("name") == null ? Counter.LABEL_NULL : m
								.get("name"));
				p.data().add(m.get("value"));
			}
			option.xAxis().add(x);
		}

		option.series().add(p);
		return option;

	}

	protected GsonOption resolveRatio() {

		queryRequest.setWhere(queryRequest.getWhere() + sb.toString());

		queryRequest.setOrderBy("value DESC");

		List<Map<String, Object>> list = queryRequest.query();

		GsonOption option = new GsonOption();

		CategoryAxis x = new CategoryAxis();
		Series<?> p = null;
		if (group == null)
			p = new Gauge();
		else
			p = new Bar();
		for (Map<String, Object> m : list) {

			x.data().add(m.get("name"));
			
			String v="0";
			if(m.get("value")!=null)
			  v=m.get("value").toString();

			Double i2 = Double.valueOf(v);

			BigDecimal b = new BigDecimal(i2 * setting.getQueryRateValue());

			SeriesData sd = new SeriesData(b.setScale(2,
					BigDecimal.ROUND_HALF_UP).floatValue());

			p.data().add(sd);

		}

		if (group != null)
			option.xAxis().add(x);

		option.series().add(p);
		return option;

	}

	protected GsonOption resolveValueLine() {

		String gd = qll.getDateFormatQuery(setting.getQueryDateColumn());

		queryRequest.addColumn(qll.getDateFormatQuery(setting
				.getQueryDateColumn()) + "  AS xa");

		queryRequest.addColumn("COUNT(" + setting.getQueryDateColumn()
				+ ") AS dd");
		queryRequest.setWhere(sb.toString());

		GsonOption option = new GsonOption();

		if (StringUtils.isEmpty(queryRequest.getGroupBy())) {

			queryRequest.setGroupBy(gd);
		} else {
			queryRequest.setGroupBy(queryRequest.getGroupBy() + "," + gd);
		}
		queryRequest.setOrderBy(qll.getDateFormatQuery(setting
				.getQueryDateColumn()) + ",dd DESC");

		List<Map<String, Object>> list = queryRequest.query();

		CategoryAxis ax = new CategoryAxis();

		HashBasedTable<String, String, Object> ml = HashBasedTable.create();
		Map<String, Line> lineMap = new LinkedHashMap<>();

		for (Map<String, Object> m : list) {
			Object n = m.get("name");
			String name = n == null ? Counter.LABEL_NULL : n.toString();
			String xa = m.get("xa").toString();
			ml.put(name, xa, m.get("dd"));

			if (!lineMap.containsKey(name)) {
				lineMap.put(name, new Line(name));
			}
		}

		for (String name : lineMap.keySet()) {

			Line l = lineMap.get(name);

			for (String dStr : qll.getValues()) {
				Object o = ml.get(name, dStr);
				if (o == null) {
					l.data().add(0);
				} else {
					l.data().add(o);
				}
			}
		}

		ax.data().addAll(qll.getValues());

		Map<String, Line> lineM = SortAnalyzer.sortLine(lineMap);

		int i = 0;

		if (lineM != null) {

			for (String name : lineM.keySet()) {
				Line l = lineM.get(name);

				l.itemStyle(DataResolver.getItsmstyle(i));
				i++;
			}

			// if (true) {
			//
			// // lineM.put("总体",
			//
			// Line l = new Line("总体");
			// for (String dStr : qll.getValues()) {
			// int o = 0;
			// for (Map<String, Object> m : list) {
			//
			// String xa = m.get("xa").toString();
			// if (dStr.equals(xa)) {
			// o += Integer.parseInt(m.get("dd").toString());
			// }
			// }
			// l.data().add(o);
			// }
			// l.itemStyle(DataResolver.getItsmstyle(i));
			//
			// lineM.put("总体", l);
			//
			// }

			option.legend().data().addAll(lineM.keySet());
			option.series().addAll(lineM.values());
		}

		option.xAxis(ax);

		return option;
	}

	protected GsonOption resolveRatioLine() {

		String gd = qll.getDateFormatQuery(setting.getQueryDateColumn());

		queryRequest.addColumn(qll.getDateFormatQuery(setting
				.getQueryDateColumn()) + "  AS xa ");

		GsonOption option = new GsonOption();

		if (StringUtils.isEmpty(queryRequest.getGroupBy())) {
			queryRequest.setGroupBy(gd);
		} else {
			queryRequest.setGroupBy(queryRequest.getGroupBy() + "," + gd);
		}
		queryRequest.setOrderBy(qll.getDateFormatQuery(setting
				.getQueryDateColumn()) + " DESC,value DESC");

		queryRequest.setWhere(queryRequest.getWhere() + sb.toString());

		List<Map<String, Object>> list = queryRequest.query();

		CategoryAxis ax = new CategoryAxis();

		HashBasedTable<String, String, Object> ml = HashBasedTable.create();
		Map<String, Line> lineMap = new LinkedHashMap<>();

		for (Map<String, Object> m : list) {
			Object n = m.get("name");
			String name = n == null ? setting.getTitle() : n.toString();
			String xa = m.get("xa").toString();

			Double i2 = Double.valueOf(m.get("value").toString());

			BigDecimal b = new BigDecimal(i2 * setting.getQueryRateValue());

			ml.put(name, xa, b.setScale(2, BigDecimal.ROUND_HALF_UP)
					.floatValue());

			if (!lineMap.containsKey(name)) {
				lineMap.put(name, new Line(name));
			}
		}

		for (String name : lineMap.keySet()) {

			Line l = lineMap.get(name);

			for (String dStr : qll.getValues()) {
				Object o = ml.get(name, dStr);
				if (o == null) {
					l.data().add(0);
				} else {
					l.data().add(o);
				}
			}

		}

		Map<String, Line> lineM = SortAnalyzer.sortLine(lineMap);
		if (lineM == null) {
			return option;
		}
		int i = 0;
		for (String name : lineM.keySet()) {
			Line l = lineM.get(name);

			l.itemStyle(getItsmstyle(i));
			i++;
		}

		ax.data().addAll(qll.getValues());
		if (lineM.size() == 1) {
			for (String key : lineM.keySet()) {
				if (!Counter.LABEL_NULL.equals(key)) {
					option.legend().data().add(key);
				}
			}
		} else {
			option.legend().data().addAll(lineM.keySet());
		}
		option.series().addAll(lineM.values());

		option.xAxis(ax);

		return option;
	}

}
